const Model = require("./Model")
class adminUser extends Model {
  //查找
  static findOne(username, password) {
    return new Promise((resolve, reject) => {
      const sql = "SELECT * FROM `user_admin` WHERE username=? AND `password`=?"
      this.query(sql, [username, password]).then(resolve).catch(reject)
    })
  }

  //查找Cookie
  static findAllCookie(currentPage, pageSize) {
    return new Promise((resolve, reject) => {
      // const sql = `SELECT * FROM user LIMIT ${pageNum} OFFSET ${currentPage}`
      const sql = `SELECT id,cookie,username,password,expiration_time,DATE_FORMAT(update_time, '%Y-%m-%d %H:%i:%s') AS update_time ,DATE_FORMAT(crea_time, '%Y-%m-%d %H:%i:%s') AS crea_time FROM seller_cookie
      ORDER BY crea_time DESC
      LIMIT ? OFFSET ?;`
      this.query(sql, [pageSize, currentPage]).then(resolve).catch(reject)
    })
  }

  // 新增cookie
  static addCookie(cookie, username, password, expiration_time, update_time, crea_time) {
    return new Promise((resolve, reject) => {
      const sql = "INSERT INTO seller_cookie (cookie,username,password,expiration_time, update_time, crea_time) VALUES (?,?,?,?,?,?)"
      this.query(sql, [cookie, username, password, expiration_time, update_time, crea_time]).then(resolve).catch(reject)
    })
  }

  //更新Cookie
  static updateCookie(cookie, username, password, expiration_time, id, update_time) {
    return new Promise((resolve, reject) => {
      const sql = "UPDATE seller_cookie SET cookie = ? ,username = ? ,password = ? ,expiration_time = ? ,update_time = ? WHERE id = ?"
      this.query(sql, [cookie, username, password, expiration_time, update_time, id]).then(resolve).catch(reject)
    })
  }

  //删除Cookie
  static removeCookie(id) {
    return new Promise((resolve, reject) => {
      const sql = "DELETE FROM seller_cookie WHERE id = ?"
      this.query(sql, [id]).then(resolve).catch(reject)
    })
  }
}

module.exports = adminUser
